//
// Created by lwj12 on 24-8-13.
//

#ifndef STRING_STRING_H
#define STRING_STRING_H
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
namespace lwj
{
    class string
    {
    public:
        typedef char* iterator;
        typedef const char* const_iterator;
        iterator begin() { return _str; }
        iterator end() {return _str + _size;}
        const_iterator begin() const {return  _str;}
        const_iterator end() const {return _str + _size;}

        string(const char*s = "");
        string(const string& s);
        ~string();
        string& operator=(string s);

        void insert(size_t pos, char ch);
        void insert(size_t pos, const string& s);
        void push_back(char ch) {insert(_size, ch);}
        void append(const string& str) {insert(_size, str);}
        void erase(size_t pos = 0, size_t len = npos);

        const char* c_str() const {return _str;}
        size_t size() const {return _size;}

        string& operator+=(char ch);
        string& operator+=(const string& s);
        char& operator[](size_t pos) {return _str[pos];}
        const char& operator[](size_t pos) const {return _str[pos];}
        bool operator<(const string& s) const;
        bool operator>(const string& s) const;
        bool operator<=(const string& s) const;
        bool operator>=(const string& s) const;
        bool operator==(const string& s) const;
        bool operator!=(const string& s) const;

        void reserve(size_t newCapacity);

        size_t find(char ch, size_t pos = 0) const;
        size_t find(const char* str, size_t pos = 0) const;

        void swap(string& s);
        string substr(size_t pos = 0, size_t len = npos) const;

        void clear() {_str[0] = '\0'; _size = 0;}
    private:
        char* _str;
        size_t _size;
        size_t _capacity;
        const static size_t npos;
    };
    istream& operator>> (istream& is, string& str);
    ostream& operator<< (ostream& os, const string& str);
}

#endif//STRING_STRING_H
